package cn.ly817.jay.service.impl;

import cn.ly817.jay.DAO.MissionDao;
import cn.ly817.jay.DAO.NoteDao;
import cn.ly817.jay.DTO.DailyNote;
import cn.ly817.jay.DTO.NoteDetail;
import cn.ly817.jay.entity.Mission;
import cn.ly817.jay.entity.Note;
import cn.ly817.jay.enums.MissionState;
import cn.ly817.jay.enums.WeekdayName;
import cn.ly817.jay.exceptions.NoteException;
import cn.ly817.jay.exceptions.NoteMultiMissionException;
import cn.ly817.jay.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by LuoYu on 2016/12/7.
 */
@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private NoteDao noteDao;

    @Autowired
    private MissionDao missionDao;

    @Override
    public int getUnhandledNumByLeaderId(Long leaderId) {

        return noteDao.getUnhandledNumByLeaderId(leaderId);
    }

    @Override
    public List<Note> getUnhandledNotesByLeaderId(Long leaderId) {
        return noteDao.getUnhandledNotesByLeaderId(leaderId,false);
    }

    @Override
    public List<Note> getHandledNotesByLeaderId(Long leaderId) {
        return noteDao.getUnhandledNotesByLeaderId(leaderId,true);
    }

    @Override
    public void saveNewMissionNote(Note missionNote) {
        //防止重复插入
        if(noteDao.getNoteNumByMissionId(missionNote.getMissionId())==0){
            noteDao.saveNote(missionNote);
            missionDao.swiftMissionState(missionNote.getMissionId(), MissionState.PENDING.getDbState());
        }else {
            throw new NoteMultiMissionException("Mission-Note必须一一对应");
        }

    }

    /**
     * 得到本周的日志列表
     *
     */
    @Override
    public List getThisWeekDaily(long userId) {

        List<DailyNote> returns = new ArrayList<DailyNote>();
        //1-星期天 ~ 7-星期六
        //获得本周开头和结尾的日期
        Calendar c= Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.DAY_OF_WEEK,1);
        Date thisSun = c.getTime();
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        c.set(Calendar.DAY_OF_WEEK,7);
        Date thisSat = c.getTime();
        //查询本周从星期天0时到星期六24时之间的日志
        List<Note> thisWeek = noteDao.getDailyNoteByDate(thisSun,thisSat,userId);
        //把日志对象装入本周的容器中，没有日志也占一格
        for (int j = 0; j < 7; j++) {
            int count = 0;
            DailyNote dn = new DailyNote();
            //把已经存在的Note对象存入DailyNote中
            for (int i = 0; i < thisWeek.size(); i++) {
                Note n = thisWeek.get(i);
                c.setTime(n.getGenerateDate());
                int index = c.get(Calendar.DAY_OF_WEEK);
                if (index == j){
                    count++;
                    if (count<=1){
                        dn.setDailyNote(n);
                        dn.setWeekIndex(index+1);
                    }else{
                        //一天不允许有多个日志
                        throw new NoteException("一天不允许有多个日志，请检查日志添加逻辑");
                    }
                }
            }
            if (count == 0)
                dn.setWeekIndex(j+1);
            dn.setDayName(WeekdayName.nameOfIndex(dn.getWeekIndex()));
            returns.add(dn);
        }
        return returns;
    }

    @Override
    public void saveNewDailyNote(Note dailyNote) {
        noteDao.saveNote(dailyNote);
    }

    @Override
    public Note getDailyById(Long dailyId) {
        return noteDao.getDailyById(dailyId);
    }

    @Override
    public Note getNoteDetail(long noteId) {
        Note note = noteDao.getFullNoteById(noteId);
        return note;
    }

    @Override
    public Note getCheckNoteById(long noteId) {
        return noteDao.getCheckNoteById(noteId);
    }

    @Override
    public void checkSubmit(Note note) {
        noteDao.checkSubmit(note);
    }

    @Override
    public float getMonthScoreByUserId(long userId) {
        Date today = new Date();
        Calendar c= Calendar.getInstance();
        c.setTime(today);
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.DAY_OF_MONTH,1);
        Date start = c.getTime();
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date end = c.getTime();
        return noteDao.getUserDurationScore(userId,start,end);
    }
}
